package com.lemon.springboot.utils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liuli
 * @Date 2020/12/18 1:00
 */
public class NumberUtil {

    private static Map<String, DecimalFormat> formatMap = new HashMap<>();

    public static String floatFormat(double num, String pattern) {
        return floatFormat(pattern).format(num);
    }

    /**
     * 根据pattern创建DecimalFormat对象
     */
    private static final DecimalFormat floatFormat(final String pattern) {
        DecimalFormat decimalFormat = formatMap.get(pattern);
        if (decimalFormat == null) {
            synchronized (DateUtil.class) {
                if (decimalFormat == null) {
                    formatMap.put(pattern, new DecimalFormat(pattern));
                    decimalFormat = formatMap.get(pattern);
                }
            }
        }
        return decimalFormat;
    }
}
